package com.boang.stream;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;

/*
  @author: 13813
  @date: 2024/12/2 15:38
*/
public class StreamDemo {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<Student>();
        list.add(new Student("zhangsan", 20, 89));
        list.add(new Student("lisi", 30, 99));
        list.add(new Student("wangwu", 25, 79));
        list.add(new Student("zhaoliu", 60, 63));
        list.add(new Student("degang", 45, 80));
        list.add(new Student("shaobing", 33, 72));
        list.add(new Student("yunpeng", 36, 60));
//        m1(list);
//        m2(list);
//        m3(list);

        List<String> stringList = list.stream().map(new Function<Student, String>() {
            @Override
            public String apply(Student student) {
                return student.name;
            }
        }).toList();

        stringList.stream().forEach(System.out::println);
    }

    private static void m3(ArrayList<Student> list) {
        list.stream().sorted(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.score - o2.score;
            }
        }).forEach(System.out::println);

        list.stream().sorted((o1, o2) -> o1.score - o2.score).forEach(System.out::println);
    }

    private static void m2(ArrayList<Student> list) {
        list.stream().filter(
                new Predicate<Student>() {
                    @Override
                    public boolean test(Student student) {
                        return student.name.startsWith("z");
                    }
                }
        ).forEach(System.out::println);

        list.stream().filter(student -> student.name.startsWith("z")).forEach(System.out::println);
    }

    private static void m1(ArrayList<Student> list) {
        Stream<Student> stream = list.stream();

//        long count = stream.count();
//        System.out.println("count = " + count);

        /*Consumer<Student> consumer = new Consumer<>() {
            @Override
            public void accept(Student student) {
                System.out.println("student = " + student);
            }
        };
        stream.forEach(consumer);*/

        stream.forEach(new Consumer<Student>() {
            @Override
            public void accept(Student student) {
                System.out.println("student = " + student);
            }
        });

//        stream.forEach(student -> System.out.println("student = " + student) );

        //        stream.forEach(System.out::println);
    }
}
